﻿using TMPro;

namespace HEFramework
{
    public static class LegacyInfoUISystem
    {
        [EntitySystem]
        public class LegacyInfoUIAwakeSystem : AwakeSystem<LegacyInfoUI>
        {
            protected override void Awake(LegacyInfoUI _self)
            {
                _self.AddChild<UnityObjectComponent>();
            }
        }


        [EntitySystem]
        public class LegacyInfoUIDestroySystem : DestroySystem<LegacyInfoUI>
        {
            protected override void Destroy(LegacyInfoUI _self)
            {
                _self.Close();
            }
        }

        public static void OpenByID(this LegacyInfoUI _self, int _id)
        {
            _self.ID = _id;
            _self.Open();
        }


        public static void Open(this LegacyInfoUI _self)
        {
            var uc = _self.GetSceneChild<UIComponent>();
            if (uc == null)
            {
                return;
            }

            uc.PushUIAsync(eUIFlag.LegacyInfo, _ =>
            {
                _self.UIWindowBehaviour = _;
                _self.Bind();
                _self.Update(true);
            });
        }

        public static void Bind(this LegacyInfoUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.UIWindowBehaviour.GetButton("Button_Back").RegisterUpEvent(_self.Close);
        }

        public static void Update(this LegacyInfoUI _self, bool _isNew = false)
        {
            if (_self.UIWindowBehaviour == null || _self.UIWindowBehaviour.State == 0)
            {
                return;
            }

            if (_self.ID == -1)
            {
                return;
            }

            var rc = _self.GetSceneChild<ResourcesComponent>();
            var dc = _self.GetSceneChild<DataTableComponent>();
            var uc = _self.GetChild<UnityObjectComponent>();
            if (rc == null || dc == null || uc == null)
            {
                return;
            }

            if (_isNew)
            {
                string message = "{0:+0;-0}{1}";
                //加载图片
                var button = _self.UIWindowBehaviour.GetButton("Button_LegacyLotteryItem");
                var data = dc.LoadDataTable<DRLegacy>().GetData(_self.ID);
                var button_transform = button.GetTransform();

                //质量修改
                var quality_transform = button_transform.GetChild(0);
                int child_count = quality_transform.childCount;
                int type = data.QualityType;
                for (int j = 0; j < child_count; j++)
                {
                    if (type == j)
                    {
                        quality_transform.GetChild(j).gameObject.SetActive(true);
                    }
                    else
                    {
                        quality_transform.GetChild(j).gameObject.SetActive(false);
                    }
                }

                //图标

                //名字
                uc.Get<TMP_Text>(button_transform.GetChild(2).gameObject).text = data.Name;

                //元素
                var element_transform = button_transform.GetChild(3);
                for (int j = 0; j < element_transform.childCount; j++)
                {
                    element_transform.GetChild(j).gameObject.SetActive(false);
                }

                for (int j = 0; j < data.ElementIDs.Length; j++)
                {
                    switch (data.ElementIDs[j])
                    {
                        case (int)eElement.Gold:
                            element_transform.GetChild(0).gameObject.SetActive(true);
                            break;
                        case (int)eElement.Wood:
                            element_transform.GetChild(1).gameObject.SetActive(true);
                            break;
                        case (int)eElement.Water:
                            element_transform.GetChild(2).gameObject.SetActive(true);
                            break;
                        case (int)eElement.Fire:
                            element_transform.GetChild(3).gameObject.SetActive(true);
                            break;
                        case (int)eElement.Soil:
                            element_transform.GetChild(4).gameObject.SetActive(true);
                            break;
                    }
                }

                //信息
                var reward_table = dc.LoadDataTable<DRReward>();
                _self.StringBuilder.Clear();
                for (int j = 0; j < data.RewardIDs.Length; j++)
                {
                    _self.StringBuilder.AppendLine(string.Format(message, data.RewardValues[j], reward_table.GetData(data.RewardIDs[j]).Name));
                }

                uc.Get<TMP_Text>(button_transform.GetChild(4).gameObject).text = _self.StringBuilder.ToString();
                _self.StringBuilder.Clear();
            }
        }

        public static void Close(this LegacyInfoUI _self)
        {
            if (_self.UIWindowBehaviour == null)
            {
                return;
            }

            _self.ID = -1;
            _self.UIWindowBehaviour.Hide();
            _self.UIWindowBehaviour = null;
        }
    }
}